package com.opensky.qatest.ui.component.common;

import org.openqa.selenium.By;

import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * AbstractJoinModal contains base locators, verifications 
 * and some user actions
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.0
 * 
 */
public abstract class AbstractJoinModal {

    private static final String JOIN_MODAL_SHOW = "//div[@id='ui-tooltip-modal' and contains(@style, 'display: block')]//div[@id='join-modal']";

    private static final String NAME_FIELD = "//div[@id='join-modal']//input[@id='registration_user_firstName']";

    private static final String EMAIL_FIELD = "//div[@id='join-modal']//input[@id='registration_user_email']";

    private static final String PASSWORD_FIELD = "//div[@id='join-modal']//input[@id='registration_user_plainPassword']";

    private static final String CATEGORIES = "//div[@id='join-modal']//div[@class='category-follow']";

    private static final String START_SHOPPING_BTN = "//div[@id='join-modal']//input[@name='submit']";

    private static final String TERMS_LINK = "//div[@id='join-modal']//div[@class='terms']/label[contains(., 'terms')]";

    private static final String SKIP_LINK = "//div[@id='join-modal']//span[@class='modal-skip-link']";

    private static final String CURATORS_IMG = "//div[@id='join-modal']//img[contains(@src, 'thejudds')]";

    private static final String LOGIN_LINK = "//div[@id='join-modal']//a[@class='login' and text()='Log in']";

    private static final String CLOSE_BTN = "//div[@id='join-modal']//div[@class='close-link' and text()='close']";
    
    private static final String INVALID_EMAIL_ALERT = "//div[@id='join-modal']//label[@for='registration_user_email' and contains(., 'Please provide valid email address.')]";
    
    private static final String EMPTY_PASSWORD_ALERT = "//div[@id='join-modal']//label[@for='registration_user_plainPassword' and contains(., 'Please provide a password.')]";
    
    private static final String SHORT_PASSWORD_ALERT = "//div[@id='join-modal']//label[@for='registration_user_plainPassword' and contains(., 'Your password is too short. It must have 6 characters or more.')]";
    
    private static final String HEALTH_CHECKBOX = "//div[@id='join-modal']//input[@id='registration_categoryFollow_healthy-living']";
    
    private static final String STYLE_CHECKBOX = "//div[@id='join-modal']//input[@id='registration_categoryFollow_style']";
    
    private static final String FOOD_CHECKBOX = "//div[@id='join-modal']//input[@id='registration_categoryFollow_food']";
    
    private static final String DESIGN_CHECKBOX = "//div[@id='join-modal']//input[@id='registration_categoryFollow_home-design']";


    protected final WebDriverWrapper driver;

    public AbstractJoinModal(WebDriverWrapper driver) {
	this.driver = driver;
    }

    
    // ************************ Actions **************************
    
    public void clickSkip() {
	driver.waitAndClick("SKIP_LINK", By.xpath(SKIP_LINK));
    }

    public void clickClose() {
	driver.waitAndClick("CLOSE_BTN", By.xpath(CLOSE_BTN));
    }
    
    
    public void signUp(String email, String password) {
	
	driver.waitAndSendKeys("EMAIL_FIELD", By.xpath(EMAIL_FIELD), email);
	driver.waitAndSendKeys("PASSWORD_FIELD", By.xpath(PASSWORD_FIELD), password);
	driver.waitAndClick("START_SHOPPING_BTN", By.xpath(START_SHOPPING_BTN));
    }
    
    public void checkCategories() {
	driver.waitAndClick("HEALTH_CHECKBOX", By.xpath(HEALTH_CHECKBOX));
	driver.waitAndClick("STYLE_CHECKBOX", By.xpath(STYLE_CHECKBOX));
	driver.waitAndClick("FOOD_CHECKBOX", By.xpath(FOOD_CHECKBOX));
	driver.waitAndClick("DESIGN_CHECKBOX", By.xpath(DESIGN_CHECKBOX));
    }
    
    // ************************ Validations **************************
    
    public boolean isJoinModalShown() {
	driver.waitAndCheckElementPresent("JOIN_MODAL", By.xpath(JOIN_MODAL_SHOW));
	return true;
    }

    public boolean isJoinModalHidden() {
	driver.waitAndCheckElementNotPresent("JOIN_MODAL", By.xpath(JOIN_MODAL_SHOW));
	return true;
    }
    
    public boolean isJoinModalBaseElementsPresent() {
	driver.waitAndCheckElementPresent("NAME_FIELD", By.xpath(NAME_FIELD));
	driver.waitAndCheckElementPresent("EMAIL_FIELD", By.xpath(EMAIL_FIELD));
	driver.waitAndCheckElementPresent("PASSWORD_FIELD", By.xpath(PASSWORD_FIELD));
	driver.waitAndCheckElementPresent("CATEGORIES", By.xpath(CATEGORIES));
	driver.waitAndCheckElementPresent("START_SHOPPING_BTN", By.xpath(START_SHOPPING_BTN));
	driver.waitAndCheckElementPresent("TERMS_LINK", By.xpath(TERMS_LINK));
	driver.waitAndCheckElementPresent("SKIP_LINK", By.xpath(SKIP_LINK));
	driver.waitAndCheckElementPresent("CURATORS_IMG", By.xpath(CURATORS_IMG));
	driver.waitAndCheckElementPresent("LOGIN_LINK", By.xpath(LOGIN_LINK));
	driver.waitAndCheckElementPresent("CLOSE_BTN", By.xpath(CLOSE_BTN));
	return true;
    }
    
    public boolean isInvalidEmailAlertPresent() {
	driver.waitAndCheckElementPresent("INVALID_EMAIL_ALERT", By.xpath(INVALID_EMAIL_ALERT));
	return true;
    }
    
    public boolean isEmptyPasswordAlertPresent() {
	driver.waitAndCheckElementPresent("EMPTY_PASSWORD_ALERT", By.xpath(EMPTY_PASSWORD_ALERT));
	return true;
    }
    
    public boolean isShortPasswordPresent() {
	driver.waitAndCheckElementPresent("SHORT_PASSWORD_ALERT", By.xpath(SHORT_PASSWORD_ALERT));
	return true;
    }   
}
